package com.adtech.farmProducts.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.adtech.common.Common;
import com.adtech.common.http.AsyncHttpPostFormData;
import com.adtech.common.http.AsyncHttpResponse;
import com.adtech.common.http.AsyncOkHttpClient;
import com.adtech.common.http.AsyncResponseHandler;
import com.adtech.common.model.RspModel;
import com.adtech.common.widgets.load.LoadFactory;
import com.adtech.common.widgets.load.core.LoadManager;
import com.adtech.common.widgets.load.view.AbsView;
import com.adtech.farmProducts.App;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.base.BaseNfcActivity;
import com.adtech.farmProducts.model.NFCResult;
import com.adtech.farmProducts.model.SourceInfo;
import com.adtech.farmProducts.widget.LoadingView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hachi.publishplugin.activity.CertPlugin;
import com.hachi.publishplugin.bean.Result;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Time:2020/6/20
 * Created by xiaocz
 * Describe 开箱验货
 */
public class VerifyActivity extends BaseNfcActivity {

    @BindView(R.id.layout_base)
    NestedScrollView layout_base;
    @BindView(R.id.layout_content)
    LinearLayout layout_content;
    @BindView(R.id.edit_gs_name)
    TextView edit_gs_name;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_time)
    TextView tv_time;

    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_unit)
    TextView tv_unit;
    @BindView(R.id.tv_weight)
    TextView tv_weight;
    @BindView(R.id.layout_bottom)
    LinearLayout layout_bottom;


    private String searchValue;

    private void showView(SourceInfo sourceInfo) {
        tv_code.setText(searchValue);
        tv_name.setText(sourceInfo.getProductName());
        tv_unit.setText(sourceInfo.getPackUnit());
        tv_address.setText(sourceInfo.getAddress());
        edit_gs_name.setText(sourceInfo.getCompanyName());
        tv_weight.setText(sourceInfo.getWeight() + sourceInfo.getUnit());
        tv_time.setText(sourceInfo.getHandleTime());
    }

    /**
     * DATA
     */
    private LoadManager mLoadManager;

    public static void show(Context context,String searchValue) {
        Intent intent = new Intent(context, VerifyActivity.class);
        intent.putExtra("searchValue",searchValue);
        context.startActivity(intent);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        if(bundle!=null){
            searchValue=bundle.getString("searchValue");
        }
        return super.initArgs(bundle);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_verify;
    }


    @Override
    protected void initWidgets() {
        super.initWidgets();
        mLoadManager = LoadFactory.getInstance().register(layout_base, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
                searchInfo();
            }
        });
        mLoadManager.showSuccessView();
    }


    @Override
    protected void initData() {
        if(searchValue!=null && !"".equals(searchValue)){
            searchInfo();
        }
        onDialogNFC();
        super.initData();
    }

    private void searchInfo() {
        loadingDialogShow(false);
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("sourceCode", searchValue);
        okHttpClient.posts(Common.Constant.API_IP + "handle/queryBySourceCode", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                onDismiss();
                layout_content.setVisibility(View.GONE);
                layout_bottom.setVisibility(View.GONE);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                onDismiss();
                Type type = new TypeToken<RspModel<SourceInfo>>() {
                }.getType();
                RspModel<SourceInfo> rspModel = getAnalysis(response, type, "溯源详情");
                if (rspModel == null) {
                    return;
                }
                switch (rspModel.getCode()) {
                    case 1:
                        if (rspModel.getData() != null) {
                            layout_content.setVisibility(View.VISIBLE);
                            layout_bottom.setVisibility(View.VISIBLE);
                            showView(rspModel.getData());
                        } else {
                            layout_content.setVisibility(View.GONE);
                            layout_bottom.setVisibility(View.GONE);
                        }
                        break;
                    default:
                        App.getInstance().showMessage(rspModel.getMessage());
                        layout_content.setVisibility(View.GONE);
                        layout_bottom.setVisibility(View.GONE);
                        break;
                }
            }
        });
    }

    @OnClick({R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                VerifySealingActivity.show(mContext, searchValue);
                finish();
                break;
//            case R.id.btn_ok:
//                String valueCodes = String.valueOf(tv_code.getText()).trim();
//                if (!TextUtils.isEmpty(valueCodes)) {
//                    searchValue = valueCodes;
//                    searchInfo();
//                } else {
//                    App.getInstance().showMessage("请输入破损的溯源码!");
//                }
//                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        NFCResult result = resolveVerifyIntent(detectedTag);
        if (result != null) {
            if(result.isState()){
                searchValue=result.getResult().getUid();
                tv_code.setText(searchValue);
                if(dialog!=null){
                    dialog.dismiss();
                }
                searchInfo();
            }else {
                App.getInstance().showMessage(result.getMessage());
            }
        }
    }
}
